﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;

public enum StepState
{
    Stop = 0,
    AddSpeed = 1,
    ConstantSpeed = 2,
    ReduceSpeed = 3
}

public class BoxRunManager : MonoBehaviour {

    public const int FruitNum = 24;

    public const int AddSpeedNum = 24;
    public const int ReduceSpeedNum = 10;

    public ImageBox box1;
    public ImageBox box2;
    public ImageBox box3;
    public ImageBox box4;
    public List<Transform> lstPosition;

    private int m_currentPos = 0;
    private int m_constSpeedNum = 0;
    private StepState m_stepState = StepState.Stop;

    private FruitEnumType fruitEnumType;


    public delegate void OnFruitStopDelegate(FruitEnumType fruitEnumType);
    public OnFruitStopDelegate OnAddFruit;
	// Use this for initialization
	void Start () 
    {
       // Run(0);
	}
	
	// Update is called once per frame
	void Update () 
    {
        //if (m_stepState == StepState.Stop) return;
	}

    /// <summary>
    /// 目标位置从0开始索引
    /// </summary>
    /// <param name="tagPos"></param>
    public void Run(int tagPos, FruitEnumType _fruitEnumType)
    {
        fruitEnumType = _fruitEnumType;
        if (m_stepState == StepState.Stop)
        {
            int totalNum =UnityEngine.Random.Range(4,5) * FruitNum;
            m_constSpeedNum = totalNum - AddSpeedNum - ReduceSpeedNum  + (tagPos - m_currentPos);
            StartCoroutine(StartAddSpeed());
            m_stepState = StepState.AddSpeed;
        }


       // int totalNum = FruitNum * 5 - AddSpeedNum - ReduceSpeedNum - m_currentPos;
        
    }

    /// <summary>
    /// 加速过程
    /// </summary>
    /// <returns></returns>
    IEnumerator StartAddSpeed()
    {
        float startTime = 0.15f;
        float endTime = 0.015f;
        float preTime = startTime;
        for (int index = 0; index < AddSpeedNum; index++)
        {
            preTime = Mathf.Lerp(preTime, endTime, 0.2f);
            m_currentPos++;
            if (m_currentPos >= FruitNum)
            {
                m_currentPos = 0;
            }

            SetPos(m_currentPos);
            if (index == 0)
            {
                box2.gameObject.SetActive(true);
            }
            if (index == 1)
            {
                box3.gameObject.SetActive(true);
            }
            if (index == 2)
            {
                box4.gameObject.SetActive(true);
            }
            SoundController.Instance.PlaySoundFromResource("Sound/s" + (m_currentPos % 8 + 1));
            yield return new WaitForSeconds(preTime);
        }

        StartCoroutine(ConstantSpeed(preTime));
    }

    /// <summary>
    /// 匀速过程
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    IEnumerator ConstantSpeed(float time)
    {
        int totalNum = m_constSpeedNum;

        for (int index = 0; index < totalNum; index++)
        {
            m_currentPos++;
            if (m_currentPos >= FruitNum)
            {
                m_currentPos = 0;
            }
            SetPos(m_currentPos);
            SoundController.Instance.PlaySoundFromResource("Sound/run");
            yield return new WaitForSeconds(time);
        }

        StartCoroutine(ReduceSpeed(time));
    }

    /// <summary>
    /// 减速过程
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    IEnumerator ReduceSpeed(float time)
    {
        float endTime = 0.4f;
        float preTime = time;
        for (int index = 0; index < ReduceSpeedNum; index++)
        {
            preTime =  Mathf.Lerp(preTime, endTime, 0.1f);
            m_currentPos++;
            if (m_currentPos >= FruitNum)
            {
                m_currentPos = 0;
            }

            SetPos(m_currentPos);
            
            if (index == 6)
            {
                box2.gameObject.SetActive(false);
            }
            if (index == 4)
            {
                box3.gameObject.SetActive(false);
            }
            if (index == 2)
            {
                box4.gameObject.SetActive(false);
            }
            SoundController.Instance.PlaySoundFromResource("Sound/run");
            if(index>=8)
               preTime += 0.4f;
            yield return new WaitForSeconds(preTime);
        }
        m_stepState = StepState.Stop;
        OnAddFruit(fruitEnumType);
    }

    /// <summary>
    /// 设置位置
    /// </summary>
    /// <param name="pos"></param>
    void SetPos(int pos)
    {
        box1.transform.localPosition = lstPosition[pos].transform.localPosition;

        int former = pos;
        former = GetPrePos(former);
        box2.transform.localPosition = lstPosition[former].transform.localPosition;

        former = GetPrePos(former);
        box3.transform.localPosition = lstPosition[former].transform.localPosition;

        former = GetPrePos(former);
        box4.transform.localPosition = lstPosition[former].transform.localPosition;
    }

    /// <summary>
    /// 获取上一个位置点
    /// </summary>
    /// <param name="tagPos"></param>
    /// <returns></returns>
    public int GetPrePos(int tagPos)
    {
        if (tagPos == 0)
        {
            return FruitNum - 1;
        }
        return tagPos - 1;
    }

}
